package com.welove.util;


import java.io.IOException;
import java.io.InputStream;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;

public class ImageUtil {

	private Context mContext;
	
	public ImageUtil(Context mContext) {
		super();
		this.mContext = mContext;
	}

	// 根据uri读取图片

	public Bitmap readBitmapFromUri(Uri uri)
	{
		
		
		Options options = new BitmapFactory.Options(); 
		
		options.inJustDecodeBounds = true; 
		options.inSampleSize = 2; 
	    int outWidth  = options.outWidth; 
	    int outHeight = options.outHeight; 
	    Bitmap bmap = decode(uri, options);
	    float imagew = 150; 
	    float imageh = 150; 
	    int yRatio = (int) Math.ceil(outHeight 
	            / imageh); 
	    int xRatio = (int) Math 
	            .ceil(outWidth / imagew); 
	    if (yRatio > 1 || xRatio > 1) { 
	        if (yRatio > xRatio) { 
	        	options.inSampleSize = yRatio; 
	        } else { 
	        	options.inSampleSize = xRatio; 
	        } 
	 
	    }  
	    options.inJustDecodeBounds = false; 
	    options.inPreferredConfig=Bitmap.Config.RGB_565;
		options.inPurgeable = true;   
		options.inInputShareable = true;  
	    //下面这个设置是将图片边界不可调节变为可调节 
	  
	    bmap=decode(uri, options);
	    
	    return bmap; 
		
	}
	
	 public Bitmap decode(Uri uri, BitmapFactory.Options options) {  
	  
	        Bitmap result = null;  
	  
	      
	            //uri不为空的时候context也不要为空.  
	            ContentResolver cr = mContext.getContentResolver();  
	            InputStream inputStream = null;  
	  
	            try {  
	                inputStream = cr.openInputStream(uri);  
	                result = BitmapFactory.decodeStream(inputStream, null, options);  
	                inputStream.close();  
	            } catch (Exception e) {  
	                e.printStackTrace();  
	            }  
	            
	            return result;
	  
	 }
	
	// 读取相机旋转角度
	private int readPictureDegree(String path) {
		int degree = 0;
		try {
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_NORMAL);
			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				degree = 90;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				degree = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				degree = 270;
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return degree;
	}

	// 旋转图片
	public Bitmap rotaingImageView(Uri uri,Bitmap oriBm) {

		// 旋转图片 动作
		Matrix matrix = new Matrix();
		matrix.postRotate(readPictureDegree(uri.getPath()));

		// 创建新的图片
		Bitmap resizedBitmap = Bitmap.createBitmap(oriBm, 0, 0,
				oriBm.getWidth(), oriBm.getHeight(), matrix, true);
		return resizedBitmap;
	}

	// 缩放图片
	public Bitmap resizeBitmap(Bitmap oriBm) {

		Bitmap newBm = null;

		int width = oriBm.getWidth();
		int height = oriBm.getHeight();
		// 设置想要的大小

		int newWidth = StringUtil.dip2px(mContext, 70);
		int newHeight = StringUtil.dip2px(mContext, 67);
		// 计算缩放比例
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的图片
		newBm = Bitmap.createBitmap(oriBm, 0, 0, width, height, matrix, true);

		return newBm;

	}

}
